package com.pro.schedule.controller;

import com.pro.schedule.entity.User;
import com.pro.schedule.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@RestController
public class UserController {
    @Resource
    UserService userService;
    @ApiOperation("用户登录")
    @PostMapping("/schedule/user/login")
    public String loginUser(@RequestBody User user) throws Exception {
        return userService.loginUser(user);
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/schedule/user/getInfo")
    public User getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        return userService.getUserInfo(token);
    }

    @ApiOperation("注册或更新用户信息")
    @PostMapping("/schedule/user/saveOrUpdate")
    public String saveOrUpdateUser(@RequestBody User user,HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return userService.saveOrUpdateUser(user,token);
    }

    @ApiOperation("修改用户密码")
    @PostMapping("/schedule/user/changePassword")
    public String changePassword(@RequestBody User user, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        return userService.changePassword(token, user.getNewPassword(), user.getUserPassword());
    }

    @ApiOperation("用户忘记密码")
    @PostMapping("/schedule/user/forgetPassword")
    public String forgetPassword(@RequestBody User user) throws Exception {
        return userService.forgetPassword(user.getUserPhone(), user.getNewPassword());
    }

    @ApiOperation("返回手机验证码")
    @PostMapping("/schedule/user/getCode")
    public String getCode(@RequestBody User user) throws Exception {
        return userService.getCode(user.getUserPhone());
    }

    @ApiOperation("上传头像")
    @PostMapping("/schedule/user/avatar")
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        String path=request.getServletContext().getRealPath("/upload/");
        String token = request.getHeader("Authorization");
        return userService.uploadAvatar(file,path,token);
    }
}
